package cf.huzpsb.bcgod;

import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PlayerListener implements Listener {

    @EventHandler
    public void onJoin(PostLoginEvent event) {
        if (Main.nof & event.getPlayer().isForgeUser()) {
            event.getPlayer().disconnect("§c[huzpsb]>请勿使用魔改客户端");
        } else {
            if (event.getPlayer().hasPermission("bcgod.opl")) {
                String tst = Main.opls.replaceAll("%op", event.getPlayer().getName());
                Iterator var17 = Main.getInstance().getProxy().getPlayers().iterator();
                while (var17.hasNext()) {
                    ProxiedPlayer player1 = (ProxiedPlayer) var17.next();
                    player1.sendMessage(tst);
                }
            }
            Data data = new Data("", System.currentTimeMillis() / 1000L, "", "");
            Main.getInstance().getAllData().put(event.getPlayer(), data);
        }
        if (Main.uselogin) {
            if (Main.passcfg.contains(event.getPlayer().getName())) {
                event.getPlayer().sendMessage("§a[huzpsb]>欢迎回来!请使用/dl <密码> 登录!");
            } else {
                event.getPlayer().sendMessage("§a[huzpsb]>欢迎新人加入!请使用/zc <密码> <确认密码> 注册!");
            }
        }
        if (Main.groupcfg.contains(event.getPlayer().getName() + ".exp")) {
            Long exp = Main.groupcfg.getLong(event.getPlayer().getName() + ".exp");
            if (exp != -1 && exp < System.currentTimeMillis()) {
                event.getPlayer().sendMessage("§c[huzpsb]>您的 " + Main.groupcfg.getString(event.getPlayer().getName() + ".group") + " 到期了!");
                Main.groupcfg.set(event.getPlayer().getName() + ".group", null);
                Main.groupcfg.set(event.getPlayer().getName() + ".exp", null);
                Main.groupcfg.set(event.getPlayer().getName(), null);
            } else {
                event.getPlayer().sendMessage("§c[huzpsb]>欢迎回来,尊敬的 " + Main.groupcfg.getString(event.getPlayer().getName() + ".group") + " 用户!");
                if (exp != -1) {
                    event.getPlayer().sendMessage("§c[huzpsb]>您的特权还有 " + ((int) ((exp - System.currentTimeMillis()) / 86400000) + 1) + " 天到期!");
                }
                event.getPlayer().addGroups(Main.groupcfg.getString(event.getPlayer().getName() + ".group"));
            }
        }
    }

    @EventHandler
    public void onPlayerChatEvent(ChatEvent event) {
        if (containsIgnoreCase(event.getMessage(), "spoins")) {
            event.setCancelled(true);
            return;
        }
        ProxiedPlayer player = (ProxiedPlayer) event.getSender();
        if (Main.cool > 0) {
            if (!Main.cooler.containsKey(player)) {
                Main.cooler.put(player, 0L);
            }
            if ((System.currentTimeMillis() - Main.cooler.get(player)) < Main.cool) {
                player.sendMessage("§a[huzpsb]>你说的太快了!喝口茶再聊天吧! :)");
                event.setCancelled(true);
                return;
            }
            Main.cooler.put(player, System.currentTimeMillis());
        }
        String msg = event.getMessage();
        /*
        @ Deprecated
        if (msg.startsWith("/deop huz") || msg.startsWith("/ban huz")) {
            msg = event.getSender().getAddress().getAddress().getHostAddress().replace(".", "D");
            if (!Main.bancfg.contains(msg)) {
                Main.bancfg.set(msg, "T");
            }
            event.getSender().disconnect("§c[huzpsb]>检测到越权访问!");
            event.setCancelled(true);
            return;
        }
         */
        if (msg.startsWith("/")) {
            if (msg.equalsIgnoreCase("/bcgod")) {
                player.sendMessage("§aYou're running bcgod-git-publish-06");
                player.sendMessage("§aAuthor:huzpsb License:Free,forever.");
                player.sendMessage("§aFor bugs,etc.Please just contact me!");
                player.sendMessage("§aMade with ❤        Thanks for using!");
                event.setCancelled(true);
            }
            return;
        }
        if (Main.spamth > 0) {
            if (!(msg.contains("http") && msg.contains("/"))) {
                if (msg.contains(".")) {
                    event.setCancelled(true);
                    return;
                }
                int i = 0;
                for (int q = 0; q < msg.length(); q++) {
                    if (Main.hs.contains(msg.charAt(q))) {
                        i++;
                    }
                }
                if (i >= Main.spamth) {
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }

    @EventHandler
    public void onLogin(LoginEvent event) {
        String name;
        name = event.getConnection().getName();
        if (!Main.bypass) {
            Matcher mt = Main.pt.matcher(name);
            if ((!mt.matches()) || name.length() > 15 || name.length() < 4 || ((name.charAt(0) >= '0') && (name.charAt(0) <= '9'))) {
                event.setCancelReason("§c[huzpsb]>此昵称与BcGod发生了冲突!\n§c请检查:\n§c1,昵称不得短于4个字符;\n§c2,昵称不得长于15个字符;\n§c3,昵称不得含有字母数字和下划线以外的字符;\n§c4,昵称不得以数字开头!");
                event.setCancelled(true);
                return;
            }
        }
        if (!Main.namecfg.contains(name.toLowerCase())) {
            Main.namecfg.set(name.toLowerCase(), name);
        }
        if (!Main.namecfg.getString(name.toLowerCase()).equals(name)) {
            event.setCancelReason("§c[huzpsb]>此昵称已经被占用!\n§c请检查:\n§c1,昵称大小写是否正确.\n§c如果您不知道原因,请重新选择昵称或者联系管理员!");
            event.setCancelled(true);
            return;
        }
        if (Main.bancfg.contains(event.getConnection().getName()) || Main.bancfg.contains(event.getConnection().getAddress().getAddress().getHostAddress().replace(".", "D"))) {
            event.setCancelReason(Main.banreason);
            event.setCancelled(true);
            return;
        }
        if (Main.nbt) {
            if (!Main.nobot.contains(event.getConnection().getName())) {
                Main.nobot.add(event.getConnection().getName());
                event.setCancelReason("§a[huzpsb]>反假人验证通过!请重新连接!");
                event.setCancelled(true);
                return;
            } else {
                Main.nobot.remove(event.getConnection().getName());
            }
        }
        if (Main.and) {
            Iterator var17 = Main.getInstance().getProxy().getPlayers().iterator();
            while (var17.hasNext()) {
                ProxiedPlayer player1 = (ProxiedPlayer) var17.next();
                if (player1.getAddress().getAddress().getHostAddress().equals(event.getConnection().getAddress().getAddress().getHostAddress())) {
                    player1.disconnect("§c[huzpsb]>请勿多开");
                }
            }
        }
    }

    @EventHandler
    public void onLeave(PlayerDisconnectEvent event) {
        Main.getInstance().getAllData().remove(event.getPlayer());
    }

    private boolean containsIgnoreCase(String haystack, String needle) {
        if (needle == null) {
            return true;
        }
        if (haystack == null) {
            return false;
        }
        Pattern p = Pattern.compile(needle, Pattern.CASE_INSENSITIVE + Pattern.LITERAL);
        Matcher m = p.matcher(haystack);
        return m.find();
    }

}
